<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    <meta name="description" content="">
    <meta name="author" content="ThemeBucket">
    <link rel="shortcut icon" href="/images/favicon.ico"  type="image/x-icon">
    <title>用户列表</title>

    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    <link th:href="@{/js/iCheck/skins/minimal/minimal.css}" rel="stylesheet">
    <link th:href="@{/js/iCheck/skins/minimal/minimal.css}" rel="stylesheet">
    <link th:href="@{/js/iCheck/skins/square/square.css}" rel="stylesheet">
    <link href="/js/iCheck/skins/square/red.css" rel="stylesheet">
    <link href="/js/iCheck/skins/square/blue.css" rel="stylesheet">
    <!--dashboard calendar-->
    <link href="/css/clndr.css" rel="stylesheet">
    <!--common-->
    <link href="/css/style.css" rel="stylesheet">
    <link href="/css/style-responsive.css" rel="stylesheet">
</head>

<body class="sticky-header">

<!--左侧开始-->
<div th:replace="~{admin/commons/common :: commonleft}"></div>
<!--左侧结束-->

<!--主页面开始-->
<div class="main-content" >
    <!--头部开始-->
    <div th:replace="~{admin/commons/common :: commonheader}"></div>
    <!--头部结束-->


    <!--当前位置介绍开始-->
    <div class="page-heading">
        <h3>
            用户列表
        </h3>
        <ul class="breadcrumb">
            <li>
                <a href="#">用户管理</a>
            </li>
            <li class="active"> 用户列表 </li>
        </ul>
    </div>
    <!-- 当前位置介绍结束-->


    <!--body wrapper start-->
    <div class="wrapper">
        <div class="row">
            <div class="col-sm-12">
                <section class="panel">
                    <header class="panel-heading">
                        <form class="form-horizontal adminex-form" method="get">
                            <div class="form-inline">
                                <input type="text" value="" name="searchName" class="form-control input-sm" placeholder="请输入关键字" style="font-weight:lighter">
                                <button class="btn btn-default btn-sm" th:href="@{${jumpUrl}+${qName}+${searchName}+${qPerms}}">查询</button>
                                <div class="btn-group btn-sm">
                                    <button  class="btn btn-success dropdown-toggle btn-sm" th:if="${permsValue} != null and ${#strings.trim(permsValue)} eq ''" data-toggle="dropdown"  type="button">权限 <span class="caret"></span></button>
                                    <button  class="btn btn-success dropdown-toggle btn-sm" th:if="${permsValue} != null and ${#strings.trim(permsValue)} eq 'admin' " data-toggle="dropdown"  type="button">管理员 <span class="caret"></span></button>
                                    <button  class="btn btn-success dropdown-toggle btn-sm" th:if="${permsValue} != null and ${#strings.trim(permsValue)} eq 'user'" data-toggle="dropdown"  type="button">用户 <span class="caret"></span></button>
                                    <ul role="menu" class="dropdown-menu">
                                        <li ><a th:href="@{${jumpUrl}+${qName}+${searchName}+${qPerms}+'admin'}">管理员</a></li>
                                        <li ><a th:href="@{${jumpUrl}+${qName}+${searchName}+${qPerms}+'user'}">用户</a></li>
                                    </ul>
                                </div>
                                <button type="button" class="btn btn-sm btn-info" data-toggle="modal" href="#addModal"><i class="fa fa-plus"></i>添加用户</button>
                                <button class="btn btn-sm btn-danger" onclick="DelSelect()"><i class="fa fa-archive" ></i>批量删除</button>
                            </div>
                        </form>
                    </header>

                    <div class="panel-body">
                        <div class="table-responsive" style="height: 55em">
                            <div class="space15"></div>
                            <table class="display table table-hover" id="dynamic-table">
                                <thead>
                                <tr>
                                    <th><input type="checkbox" name="id[]" id="check" onclick="checkAll(this,'id[]');"/> <a type="button" onclick="$('#check').click()" href="javascript:void(0);" >全选</a></th>
                                    <th>id </th>
                                    <th>用户名</th>
                                    <th>密码</th>
                                    <th>学号</th>
                                    <th>真实姓名</th>
                                    <th>性别</th>
                                    <th>专业</th>
                                    <th>权限</th>
                                    <th>创建时间</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr th:each="user : ${page.records}">
                                    <td class="center">  <input type="checkbox" name="id[]"  th:value="${user.id}" id="id[]" /></td>
                                    <td th:text="${user.id}"></td>
                                    <td th:text="${user.userName}"></td>
                                    <td  th:text="${user.password}"></td>
                                    <td th:text="${user.uId}"></td>
                                    <td th:text="${user.realName}"></td>
                                    <td th:text="${user.sex==1} ? '男' : '女'"></td>
                                    <td th:text="${user.profession}"></td>
                                    <td>
                                        <span th:if="${user.perms}==admin"  class="label label-success">管理员</span>
                                        <span th:if="${user.perms}==user" class="label label-primary">用户</span>
                                    </td>
                                    <td th:text="${#temporals.format(user.createTime, 'yyyy-MM-dd HH:mm')}"></td>
                                    <td>
                                        <button th:if="${user.uStatus}==1" type="button" th:value="${user.id}" class="btn btn-sm btn-success"  name="statusButton"  id="statusButton" ><i class="fa "></i>启用</button>
                                        <button th:if="${user.uStatus}==0" type="button" th:value="${user.id}" class="btn btn-sm btn-danger"  name="statusButton"  id="statusButton" ><i class="fa "></i>禁用</button>
                                    </td>
                                    <td>
                                        <a href="#updateModal" data-toggle="modal" th:onclick="values([[${user.id}]],[[${user.userName}]],[[${user.password}]],[[${user.uId}]],[[${user.realName}]],
                                    [[${user.sex}]],[[${user.profession}]],[[${user.perms}]])">edit</a>
                                        <a  th:href="@{/admin/delete/{id}(id=${user.id})}"
                                           onclick="return del()" th:value="${user.id}">Delete
                                        </a>
                                    </td>
                                </tr>
                                </tbody>

                            </table>
                            <div><!--定义用于被显示列表页包含-->
                                <head>
                                    <style>
                                        .page-item {
                                            padding: 1px 5px;
                                            border: 1px solid #afafaf;
                                            margin-left: 3px
                                        }

                                        .page-item.active {
                                            color: red;
                                            border: 1px solid red;
                                        }
                                    </style>
                                </head>
                                <div style="float: left" th:if="${page.pages>0}">
                                    当前第<span th:text="${page.current}"></span>页,共<span
                                        th:text="${page.pages}"></span>页,总记录数<span th:text="${page.total}"></span>
                                </div>
                                <div style="float: right" th:if="${page.pages>0}">
                                    <a th:href="@{${jumpUrl}+${qName}+${searchName}+${qPerms}+${permsValue}}" th:text="首页" th:if="${page.current>1}" th:class="page-item"></a>
                                    <a th:href="@{${jumpUrl}+${page.current-1}+${qName}+${searchName}+${qPerms}+${permsValue}}" th:text="上一页" th:class="page-item"
                                       th:if="${page.current>1}"></a>
                                    <a th:href="@{${jumpUrl}+${i}+${qName}+${searchName}+${qPerms}+${permsValue}}"
                                       th:each="i :${#numbers.sequence(1, page.pages)}" th:text="${i}"
                                       th:class="${page.current == i}? 'page-item active' :'page-item' "></a>
                                    <a th:href="@{${jumpUrl}+${page.current+1}+${qName}+${searchName}+${qPerms}+${permsValue}}" th:text="下一页" th:class="page-item"
                                       th:if="${page.current<page.pages}"></a> <a
                                        th:href="@{${jumpUrl}+${page.pages}+${qName}+${searchName}+${qPerms}+${permsValue}}" th:text="尾页" th:class="page-item"
                                        th:if="${page.current<page.pages}"></a>
                                </div>
                            </div>

                        </div>

                        <!--   添加功能modal框开始-->
                        <div aria-hidden="true" data-backdrop="static" data-keyboard="false"  aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="addModal" class="modal fade">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <div aria-hidden="true">&times;</div>
                                        </button>
                                        <h5 class="modal-title">新增</h5>
                                    </div>
                                    <div class="modal-body row">
                                            <div class="col-md-7">
                                                <form role="form"  class="form-horizontal"  method="post">
                                                <div class="form-group">
                                                    <label for="userName" class="col-md-3">用户名:</label>
                                                    <input type="text" id="userName" name="userName" class="col-md-6" placeholder="请输入用户名，用户名不可重复"/>
                                                </div>
                                                <div class="form-group">
                                                    <label for="password" class="col-md-3">密 码:</label>
                                                    <input type="password" id="password" name="password" class="col-md-6" placeholder="请输入密码"/>
                                                </div>
                                                <div class="form-group">
                                                    <label for="uId" class="col-md-3">学 号:</label>
                                                    <input type="text" id="uId" name="uId" class="col-md-6" placeholder="请输入学号"/>
                                                </div>
                                                <div class="form-group">
                                                    <label for="realName" class="col-md-3">姓 名:</label>
                                                    <input type="text" id="realName" name="realName" class="col-md-6" placeholder="请输入姓名"/>
                                                </div>
                                                <div class="form-group">
                                                    <label for="profession" class="col-md-3">专 业:</label>
                                                    <input type="text" id="profession" name="profession" class="col-md-6" placeholder="请输入专业" />
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-3">性 别:</label>
                                                    <input type="radio" class="control-label" value="1" name="sex" checked/>男
                                                    <input type="radio" class="control-label"  value="2" name="sex"/>女
                                                </div>
                                                <div class="form-group">
                                                    <label  class="col-md-3">权 限:</label>
                                                    <input type="radio" class="control-label" value="user" name="perms" checked/>用户
                                                    <input type="radio" class="control-label"  value="admin" name="perms" />管理员
                                                </div>
                                                </form>
                                            </div>
                                            <div class="modal-footer col-md-12">
                                                <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
                                                <button type="button" class="btn btn-success" onclick="addUser()">保存</button>
                                            </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!--   添加modal框结束-->

                        <!-- 修改modal框开始   -->
                        <div aria-hidden="true"  data-backdrop="static" data-keyboard="false" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="updateModal" class="modal fade" >
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <div aria-hidden="true">&times;</div>
                                        </button>
                                        <h5 class="modal-title">修改</h5>
                                    </div>
                                    <div class="modal-body row">
                                        <form role="form" id="updateForm" class="form-horizontal" th:action="@{/admin/updateUser}" method="post">
                                            <input type="hidden" name="id" id="id" value=""/>
                                            <div class="col-md-7">
                                                <div class="form-group">
                                                    <label for="userNameUpdate" class="col-md-3">用户名:</label>
                                                    <input type="text" id="userNameUpdate" name="userNameUpdate" class="col-md-6"  readonly/>
                                                </div>
                                                <div class="form-group">
                                                    <label for="passwordUpdate" class="col-md-3">密 码:</label>
                                                    <input type="text" id="passwordUpdate" name="passwordUpdate" class="col-md-6"  />
                                                </div>
                                                <div class="form-group">
                                                    <label for="uIdUpdate" class="col-md-3">学 号:</label>
                                                    <input type="text" id="uIdUpdate" name="uIdUpdate" class="col-md-6" />
                                                </div>
                                                <div class="form-group">
                                                    <label for="realNameUpdate" class="col-md-3">姓 名:</label>
                                                    <input type="text" id="realNameUpdate" name="realNameUpdate" class="col-md-6"/>
                                                </div>
                                                <div class="form-group">
                                                    <label for="professionUpdate" class="col-md-3">专 业:</label>
                                                    <input type="text" id="professionUpdate" name="professionUpdate" class="col-md-6" />
                                                </div>
                                                <div class="form-group">
                                                    <label  class="col-md-3">性 别:</label>
                                                    <div class="form-inline">
                                                        <label for="sexUpdate1">
                                                            <input  type="radio" class="control-label" value="1" id="sexUpdate1" name="sexUpdate"  required/>男
                                                        </label>
                                                        <label for="sexUpdate2">
                                                            <input  type="radio" class="control-label"  value="2"  id="sexUpdate2" name="sexUpdate" required/>女
                                                        </label>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-3">权 限:</label>
                                                    <div class="form-inline">
                                                        <label for="permsUpdate1">
                                                            <input  type="radio" class="control-label" value="user" id="permsUpdate1" name="permsUpdate" required/>用户
                                                        </label>
                                                        <label for="permsUpdate2">
                                                            <input  type="radio" class="control-label"  value="admin"  id="permsUpdate2" name="permsUpdate" required/>管理员
                                                        </label>
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="modal-footer col-md-12">
                                                <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
                                                <button type="submit" class="btn btn-success" >保存</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!--   修改modal框结束-->


                    </div>
                </section>

            </div>
        </div>
    </div>
    <!--body wrapper end-->

    <!--footer section start-->
    <footer>
        2022 &copy;<a href="https://www.pdsu.edu.cn/" target="_blank">平顶山学院软件应用双创实验室提供技术支持</a>
    </footer>
    <!--footer section end-->


</div>
<!--主页面结束-->

<div th:replace="~{admin/commons/common :: commonupdatePwd}"></div>

<!-- Placed js at the end of the document so the pages load faster -->
<script src="/js/jquery-1.10.2.min.js"></script>
<script src="/js/jquery-ui-1.9.2.custom.min.js"></script>
<script src="/js/jquery-migrate-1.2.1.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/modernizr.min.js"></script>
<script src="/js/jquery.nicescroll.js"></script>

<!--data table-->
<script type="text/javascript" src="/js/data-tables/jquery.dataTables.js"></script>
<script type="text/javascript" src="/js/data-tables/DT_bootstrap.js"></script>

<!--表单校验-->
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script src="js/validation-init.js"></script>

<!--common scripts for all pages-->
<script src="/js/scripts.js"></script>
<!--script for editable table-->
<script src="/js/editable-table.js"></script>

<script src="/js/dropzone/dropzone.js"></script>

<script type="text/javascript">


    function checkAll(obj,cName){
        var checkboxs = document.getElementsByName(cName);
        for(var i = 0; i< checkboxs.length; i+=1){
            checkboxs[i].checked = obj.checked;
        }
    }

    function addUser() {
        var userName = $("#userName").val()
        var password = $("#password").val()
        var uId = $("#uId").val()
        var realName = $("#realName").val()
        var profession = $("#profession").val()
        var sex = $("input[name='sex']:checked").val();
        var perms = $("input[name='perms']:checked").val();

        var user= {"userName" : userName,
            "password" : password,
            "realName":realName,
            "uId":uId,
            "profession": profession,
             "sex":sex,
             "perms":perms
        };
        console.log(user)

        $.ajax({
            url: "/admin/addUser?uId="+uId,
            //请求类型
            type: "POST",
            //data表示发送的数据
            data: JSON.stringify(user), //定义发送请求的数据格式为JSON字符串
            contentType: "application/json;charset=utf-8",
            //定义回调响应的数据格式为JSON字符串，该属性可以省略
            dataType: "json",
            //成功响应的结果
            success: function (data) {
                if(data.code === 200){
                    alert(data.msg)
                    location.reload(true);
                }else{
                    alert(data.msg)
                }
            }
        })
    }


    // 单个删除
    function del() {
        if (confirm("您确定要删除吗？")) {
            return true;
        } else {
            return false;
        }
    }

    // 批量删除
    function DelSelect() {
        var Checkbox = false;
        $("input[name='id[]']").each(function() {
            if (this.checked == true) {
                Checkbox = true;
            }
        });
        if (Checkbox) {
            var t = confirm("您确认要删除选中的内容吗？");
            if (t == false) {
                return false;
            } else {
                var ids="";
                $("input[name='id[]']").each(function() {
                    if (this.checked == true) {
                        ids += this.value + ",";
                    }
                });
                window.location.replace("/admin/deleteCounts?ids="+ids);
            }
        } else {
            alert("请选择您要删除的内容!");
            return false;
        }
    }
    // //获取按钮数组
    // var len = document.getElementsByName("eidtButton").length;
    // //通过循环给每个按钮添加点击事件
    // var temp = document.getElementsByName("eidtButton")
    // // alert(len)
    // for (var i=0;i < document.getElementsByName("eidtButton").length; i++){
    //     document.getElementsByName("eidtButton")[i].onclik= (function(){
    //         var id = this.value;
    //
    //     })
    // }


    //获取按钮数组
    var len = document.getElementsByName("statusButton").length;
    //通过循环给每个按钮添加点击事件
    var temp = document.getElementsByName("statusButton")
    // alert(len)

    for(var i = 0; i < document.getElementsByName("statusButton").length ; i++){
        document.getElementsByName("statusButton")[i].onclick = (function() {
            var id = this.value
            var statusValue = this.innerText=='启用'?1:0
            var _this = $(this)
            $.ajax({
                //请求路径
                url: "/admin/statusChange",
                //请求类型
                type: "POST",
                //data表示发送的数据
                data: JSON.stringify({
                    uStatus: statusValue,
                    id: id
                }), //定义发送请求的数据格式为JSON字符串
                contentType: "application/json;charset=utf-8",
                //定义回调响应的数据格式为JSON字符串，该属性可以省略
                dataType: "json",
                //成功响应的结果
                success: function (data) {
                    if(data.status==1){
                        _this.removeClass('btn-danger').addClass('btn-success').html('启用')
                    }else if(data.status==0) {
                        _this.removeClass('btn-success').addClass('btn-danger').html('禁用')
                    }
                    else {
                        alert('修改失败')
                    }
                },

            })
        });
    }

    function values(id,userName,password,uId,realName,sex,profession,perms){
        $('#id').val(id)
        $('#userNameUpdate').val(userName)
        $('#passwordUpdate').val(password)
        $('#uIdUpdate').val(uId)
        $('#realNameUpdate').val(realName)
        $("input[name='sexUpdate'][value='" + sex + "']").prop("checked", true);
        $('#professionUpdate').val(profession)
        $("input[name='permsUpdate'][value='" + perms + "']").prop("checked", true);
    }


    // 修改密码
    $("#pwd_btn").click(function() {
        $("#pwd_btn").blur();
        if (confirm("是否确定修改?")){
            var submit = true;
            var oldPwd = $("#oldPwd").val(); //原始密码
            var newPwd = $("#newPwd").val(); //新密码1
            var newPwd2 = $("#newPwd2").val(); //新密码2

            if ((oldPwd && newPwd && newPwd2) == '') {
                $("#tip").html("密码不能为空");
                $("#tip").show();
                submit = false;
            }
            if(submit){
                $.ajax({
                    type:"post",
                    url:"/admin/updatePwd",
                    data: JSON.stringify({
                        oldPwd : oldPwd,
                        newPwd : newPwd,
                        newPwd2 : newPwd2
                    }),
                    contentType: "application/json;charset=utf-8",
                    //定义回调响应的数据格式为JSON字符串，该属性可以省略
                    dataType: "json",
                    success:function (data){
                        if(data.code == 200 ){
                            $("#tip").hide();
                            $("#pwd_form").submit();
                            alert("修改成功，请重新登录!")
                            window.location.href = "/user/logout"
                        }else if(data.code != 200){
                            $("#tip").html(data.info);
                            $("#tip").show();
                        }
                    }
                });
            }
        }
    });


jQuery(document).ready(function() {
    EditableTable.init();
    });
    document.getElementById("user-list").className="active";
    document.getElementById("menu2").className="menu-list nav-active";
</script>

</body>


</html>
    <script>
        import Form_advanced_components from "./form_advanced_components.html";
        export default {
            components: {Form_advanced_components}
        }
    </script>